/*
 * This file is part of aion-lightning <aion-lightning.org>.
 *
 * aion-lightning is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-lightning is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with aion-lightning.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.aionengine.gameserver.services;

import com.aionengine.gameserver.configs.administration.AdminConfig;
import com.aionengine.gameserver.model.gameobjects.StaticDoor;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.model.templates.staticdoor.StaticDoorState;
import com.aionengine.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE;
import com.aionengine.gameserver.utils.PacketSendUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Wakizashi
 */
public class StaticDoorService {

    private static final Logger log = LoggerFactory.getLogger(StaticDoorService.class);

    public static StaticDoorService getInstance() {
        return SingletonHolder.instance;
    }

    @SuppressWarnings("synthetic-access")
    private static class SingletonHolder {

        protected static final StaticDoorService instance = new StaticDoorService();
    }

    public void openStaticDoor(final Player player, int doorId) {
        if (player.getAccessLevel() >= 3)
            PacketSendUtility.sendMessage(player, "door id : " + doorId);

        StaticDoor door = player.getPosition().getWorldMapInstance().getDoors().get(doorId);
        if (door == null) {
            log.warn("Not spawned door worldId: " + player.getWorldId() + " doorId: " + doorId);
            return;
        }
        int keyId = door.getObjectTemplate().getKeyId();

        if (player.getAccessLevel() >= 3)
            PacketSendUtility.sendMessage(player, "key id : " + keyId);

        if (checkStaticDoorKey(player, doorId, keyId)) {
            door.setOpen(true);
        } else
            log.info("Opening door without key ...");
    }

    public void changeStaticDoorState(final Player player, int doorId, boolean open, int state) {
        StaticDoor door = player.getPosition().getWorldMapInstance().getDoors().get(doorId);
        if (door == null) {
            PacketSendUtility.sendMessage(player, "Door is not spawned!");
            return;
        }
        door.changeState(open, state);
        String currentStates = "";
        for (StaticDoorState st : StaticDoorState.values()) {
            if (st == StaticDoorState.NONE)
                continue;
            if (door.getStates().contains(st))
                currentStates += st.toString() + ", ";
        }
        if ("".equals(currentStates))
            currentStates = "NONE";
        else
            currentStates = currentStates.substring(0, currentStates.length() - 2);
        PacketSendUtility.sendMessage(player, "Door states now are: " + currentStates);
    }

    public boolean checkStaticDoorKey(Player player, int doorId, int keyId) {
        if (player.getAccessLevel() >= AdminConfig.DOORS_OPEN)
            return true;

        if (keyId == 0)
            return true;

        if (keyId == 1)
            return false;

        if (!player.getInventory().decreaseByItemId(keyId, 1)) {
            PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_CANNOT_OPEN_DOOR_NEED_KEY_ITEM);
            return false;
        }

        return true;
    }
}
